package com.fh.entity.base;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ClientMessenger {
    
    private static String formatStr = "{\"dc\":\"%s\",\"code\":\"%s\",\"biz\":\"%s\",\"msg\":\"%s\",\"ms\":%d,\"ts\":%d,\"ext\":\"%s\",\"module\":\"%s\",\"method\":\"%s\",\"data\":%s}";
    // 业务识别码
    private String code;

    // 简单描述
    private String msg;

    // 业务标识(用于终端类反射)
    private String biz;

    // 用户证书
    private String dc;
    
    //消息流水码
    private long ms;
    
    //当前时间
    private long ts;
    
    //扩展字段
    private String ext;

    // 领域模型名称
    private String module;

    // 方法名称
    private String method;

    // 业务相关数据
    private Map<String, Object> data;

    public ClientMessenger() {
        data = new HashMap<String, Object>();
        code = "0";
        biz = "";
        msg = "";
        dc = "";
        module = "";
        method = "";
        ms = 0l;
        ext = "";
        //ts = System.currentTimeMillis();  
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getBiz() {
        return biz;
    }

    public void setBiz(String biz) {
        this.biz = biz;
    }

    public String getDc() {
        return dc;
    }

    public void setDc(String dc) {
        this.dc = dc;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }
    
    public long getMs() {
        return ms;
    }

    public void setMs(long ms) {
        this.ms = ms;
    }
    
    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public void put(String key, Object value) {
        data.put(key, value);
    }

    public Object get(String key) {
        return data.get(key);
    }

    public String toString() {
        return toString(false);
    }

    public String toString(boolean exposeAnnotation) {
        //String outStr = "{\"dc\":\"%s\",\"code\":\"%s\",\"biz\":\"%s\",\"msg\":\"%s\",\"ms\":%d,\"ts\":%d,\"ext\":\"%s\",\"module\":\"%s\",\"method\":\"%s\",\"data\":%s}";
        Gson gson = exposeAnnotation ? new GsonBuilder().disableHtmlEscaping()
                .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
                : new GsonBuilder().disableHtmlEscaping().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                
        /*
        Object[] keys = data.keySet().toArray();
        if (keys.length == 1 && data.get(keys[0]) instanceof List) {
            return String.format(outStr, dc, code, biz, msg, module, method,
                    gson.toJson(data.get(keys[0])));
        }
        */
        return String.format(formatStr, dc, code, biz, msg, ms, System.currentTimeMillis(), ext, module, method,
                gson.toJson(data));
    }
    
    
    
    
    public String toString(Map<String, String> format) {
        if(format == null){
            format = new HashMap<String, String>();
            format.put("dateFormat", "yyyy-MM-dd HH:mm:ss");
        }
        
        
        //String outStr = "{\"dc\":\"%s\",\"code\":\"%s\",\"biz\":\"%s\",\"msg\":\"%s\",\"ms\":%d,\"ts\":%d,\"msg\":\"%s\",\"module\":\"%s\",\"method\":\"%s\",\"data\":%s}";
        GsonBuilder gb = new GsonBuilder();
        if(format.containsKey("dateFormat")){
            gb = gb.setDateFormat(format.get("dateFormat"));
        }
        
        Gson gson = gb.create();
        return String.format(formatStr, dc, code, biz, msg, ms, System.currentTimeMillis(), ext, module, method,
                gson.toJson(data));
    }

    public static void main(String[] args) {
        /*
         * Messenger rp = new Messenger(); rp.put("name", "张三");
         * System.out.println(rp);
         */
        
        ClientMessenger cm = new ClientMessenger();
        
        System.out.println(cm.toString());
    }

}
